package cn.xy.base.server.web;

import lombok.Data;

/**
 * 存放管理后台web的上下文信息
 *
 * @author xy
 */
public class AdminWebContext {

    private AdminWebContext() {
        throw new IllegalStateException("Utility class");
    }

    private static final ThreadLocal<PublicParameter> PUBLIC_PARAMETER_THREAD_LOCAL = ThreadLocal.withInitial(PublicParameter::new);

    /**
     * 获得公共参数
     */
    public static PublicParameter getPublicParameter() {
        return PUBLIC_PARAMETER_THREAD_LOCAL.get();
    }

    /**
     * 清除
     */
    public static void clear() {
        PUBLIC_PARAMETER_THREAD_LOCAL.remove();
    }

    /**
     * 用户IP
     */
    public static String getIp() {
        PublicParameter p = PUBLIC_PARAMETER_THREAD_LOCAL.get();
        return p.getIp();
    }

    /**
     * token
     */
    public static String getToken() {
        PublicParameter p = PUBLIC_PARAMETER_THREAD_LOCAL.get();
        return p.getToken();
    }

    public static int getAdminId() {
        PublicParameter p = PUBLIC_PARAMETER_THREAD_LOCAL.get();
        return p.getAdminId();
    }

    public static int getRoleId() {
        PublicParameter p = PUBLIC_PARAMETER_THREAD_LOCAL.get();
        return p.getRoleId();
    }

    public static String getUsername() {
        PublicParameter p = PUBLIC_PARAMETER_THREAD_LOCAL.get();
        return p.getUsername();
    }

    /**
     * 公共参数列表
     */
    @Data
    public static class PublicParameter {

        /**
         * 访问token, 系统颁发，登录后必须传入，所有用户信息将只能通过token换取
         */
        private String token;

        /**
         * 用户IP
         */
        private String ip;

        /**
         * 管理员id
         */
        private int adminId;

        /**
         * 管理员账号
         */
        private String username;

        /**
         * 角色ID
         */
        private int roleId;
    }
}
